<template>
	<view class="user-info-updata">
		<view class="list">
			<view class="item" v-for="(item,index) in labelList" :key="index">
				<view class="label" @tap="uploadImg">
					{{item.name}}
				</view>
				<view class="info">
					<view class="img-holder" v-if="item.key=='icon'" @tap="uploadImg">
						<image :src="user.icon"></image>
					</view>
					<input v-else type="text" v-model="user[item.key]" />
				</view>
			</view>
		</view>

		<view class="btn" @tap="updataUserInfo">
			更新
		</view>
	</view>
</template>

<script>
	import {
		requestUpdateMemberInfo
	} from 'api/user/user.js';
	export default {
		data() {
			return {
				user: this.$store.state.userInfo,
				labelList: [{
					name: '头像',
					key: 'icon'
				}, {
					name: '用户名',
					key: 'username'
				}, {
					name: '昵称',
					key: 'nickname'
				}, {
					name: '手机号',
					key: 'phone'
				}]
			}
		},
		methods: {
			uploadImg() {
				const _self = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						const tempFilePaths = res.tempFilePaths;
						const uploadTask = uni.uploadFile({
							url: 'https://leju.bufan.cloud/lejuClient/login/uploadAvatar',
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: function(uploadFileRes) {
								uni.showToast({
									title:"上传头像成功",
									icon:'success'
								})
								let res = JSON.parse(uploadFileRes.data)
								console.log(res);
								_self.user.icon = res.data.fileUrl;
							}
						});
					},
					error: function(e) {
						console.log(e);
					}
				});
			},
			updataUserInfo() {
				console.log(this.user);
				requestUpdateMemberInfo(this.user).then(res => {
					console.log(res);
					
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.user-info-updata {
		.list {
			margin-top: 40rpx;
			width: 100%;

			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx 0;
				width: 100%;
				border-bottom: 1px solid #e5e5e5;

				.label {
					line-height: 94rpx;
					font-size: 32rpx;
					color: #494949;
				}

				.info {
					line-height: 94rpx;
					font-size: 32rpx;
					color: #a1a1a1;
					display: flex;
					align-items: center;
					text-align: right;

					.img-holder {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						overflow: hidden;
					}

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.btn {
			position: fixed;
			left: 50%;
			bottom: 20%;
			margin-left: -184rpx;
			width: 370rpx;
			height: 88rpx;
			font-size: 32rpx;
			color: #fff;
			text-align: center;
			line-height: 88rpx;
			background: #1c4b47;
			border-radius: 16rpx;
		}
	}
</style>
